package com.app.twth.ui.b2btrailer;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.b2btrailer.oblpn.B2BTrailerLpn;
import com.app.twth.ui.b2btrailer.submit.B2bTrailerQr;
import com.app.twth.ui.checkin.FileUpload;
import com.app.twth.utils.DateUtil;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class B2BTrailerViewModel extends BaseAndroidViewModel<B2BTrailerDataModel> {
    private String TAG = getClass().getSimpleName();

    public MutableLiveData<FileUpload> fileUploadMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<ArrayList<B2BTrailer>> b2BTrailersLiveData = new MutableLiveData<>();
    public MutableLiveData<B2bTrailerQr> b2bTrailerQrLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2BTrailerLpn>> loadedObLpnLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2BTrailerLpn>> pendingObLpnLiveData = new MutableLiveData<>();
    public MutableLiveData<List<B2BTrailerLpn>> submitLiveData = new MutableLiveData<>();

    public B2BTrailerViewModel(@NonNull Application application) {
        super(application);
    }

    public void b2bTrailerPendingJobs(int type) {

//        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerPendingJobs(type + 1, new JsonCallBack<BaseResponseEntity<ArrayList<B2BTrailer>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ArrayList<B2BTrailer>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ArrayList<B2BTrailer>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    b2BTrailersLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerEntryTrailer(B2bTrailerQr b2bTrailerQr) {

        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerEntryTrailer(b2bTrailerQr, new JsonCallBack<BaseResponseEntity<B2bTrailerQr>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<B2bTrailerQr>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<B2bTrailerQr> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    B2bTrailerQr data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    b2bTrailerQr.setTrailerId(data.getTrailerId());
                    b2bTrailerQrLiveData.setValue(b2bTrailerQr);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerLoadedObLpn() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerLoadedObLpn(b2bTrailerQrLiveData.getValue().getOrderNo(), new JsonCallBack<BaseResponseEntity<List<B2BTrailerLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2BTrailerLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2BTrailerLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    if (baseResponseEntity.data == null) {
                        return;
                    }
                    loadedObLpnLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerCheckObLpn(String obLpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerCheckObLpn(b2bTrailerQrLiveData.getValue().getOrderNo(), obLpn, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    if (loadedObLpnLiveData.getValue() == null) {
                        loadedObLpnLiveData.setValue(new ArrayList<>());
                    }
                    B2BTrailerLpn b2BTrailerLpn = new B2BTrailerLpn();
                    b2BTrailerLpn.setObLpn(obLpn);
                    loadedObLpnLiveData.getValue().add(b2BTrailerLpn);
                    loadedObLpnLiveData.setValue(loadedObLpnLiveData.getValue());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerRemove(int position, int detailId) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerRemove(detailId, new JsonCallBack<BaseResponseEntity<Object>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<Object>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<Object> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    loadedObLpnLiveData.getValue().remove(position);
                    loadedObLpnLiveData.setValue(loadedObLpnLiveData.getValue());
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerSubmit() {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.b2bTrailerSubmit(b2bTrailerQrLiveData.getValue(), new JsonCallBack<BaseResponseEntity<List<B2BTrailerLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2BTrailerLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2BTrailerLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void b2bTrailerPendingObLpn() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.b2bTrailerPendingObLpn(b2bTrailerQrLiveData.getValue().getOrderNo(), new JsonCallBack<BaseResponseEntity<List<B2BTrailerLpn>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<B2BTrailerLpn>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<B2BTrailerLpn>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    if (baseResponseEntity.data == null) {
                        return;
                    }
                    pendingObLpnLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void fileUpload(String containerNo, String fileName, String path, int type, int position, boolean isSubmit, boolean showDialog) {
        isLoading.setValue(new LoadingBean(showDialog, Constant.loading));
        errorResultCode.setValue("");
        String name = "b2btrailer/" + containerNo + "-" + fileName + "-" + DateUtil.getYMD() + "-" + System.currentTimeMillis() + ".jpg";
        mModel.upload(name, path, new JsonCallBack<BaseResponseEntity<String>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<String>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<String> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    FileUpload fileUpload = new FileUpload();
                    fileUpload.setType(type);
                    fileUpload.setPosition(position);
                    fileUpload.setPath(baseResponseEntity.data);
                    fileUpload.setSubmit(isSubmit);
                    Log.e(TAG, "type():" + type + ",position:" + position);
                    fileUploadMutableLiveData.setValue(fileUpload);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(baseResponseEntity.getCode());
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(error);
            }
        });
    }

}
